package com.szeastroc.core.service.system.impl;

import com.szeastroc.core.base.service.impl.BaseServiceImpl;
import com.szeastroc.core.bean.system.SystemResource;
import com.szeastroc.core.bean.system.SystemRole;
import com.szeastroc.core.bean.system.SystemRoleResource;
import com.szeastroc.core.mapper.system.SystemRoleMapper;
import com.szeastroc.core.service.system.SystemResourceService;
import com.szeastroc.core.service.system.SystemRoleResourceService;
import com.szeastroc.core.service.system.SystemRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jie on 2018/1/9.
 */
@Service
public class SystemRoleServiceImpl extends BaseServiceImpl<SystemRole> implements SystemRoleService{


    @Autowired
    private SystemRoleMapper systemRoleMapper;
    
	@Autowired
	private SystemResourceService systemResourceService;

    @Autowired
	private SystemRoleResourceService systemRoleResourceService;

    public List<SystemRole> selectRolesByUserId(Integer userId){
        List<SystemRole> roles = systemRoleMapper.selectRolesByUserId(userId);
        for (SystemRole role : roles) {
        	List<SystemResource> systemResources = systemResourceService.getSystemResourceByRoleId(role.getId());
        	role.setSysytemResources(systemResources);
		}   
        return roles;
    }

    public void addSystemRole(SystemRole userRole,String resourceIds){
        systemRoleMapper.insertUseGeneratedKeys(userRole);
        String[] rids = resourceIds.split(",");
        for (String id : rids) {
            systemRoleResourceService.insert(new SystemRoleResource(userRole.getId(),Integer.parseInt(id)));
        }
    }
}
